﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using msBsc.AppLogic.DataAlc;

namespace msBsc.WebUi.Custom
{
    public partial class wfBusinessScope : BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUser = "", strE_MAIL = "";
            if (this.Context.Request.Cookies["USER_NME"] != null)
            {
                strUser = this.Context.Request.Cookies["USER_NME"].Value.ToString();
            }
            if (this.Context.Request.Cookies["E_MAIL"] != null)
            {
                strE_MAIL = this.Context.Request.Cookies["E_MAIL"].Value.ToString();
            }
            if (!string.IsNullOrEmpty(strUser))
            {
                ShowSetp("STEP2", strUser, strE_MAIL);
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "VALIDATE")
            {
                bool isSuccess = true;
                string strMessage = "";
                string strKeyValue = this.GetParamHtValue("key_value").Trim();
                switch (this.GetParamHtValue("key"))
                {
                    case "txtUSER_NME":
                        if (strKeyValue.Length < 5 || strKeyValue.Length > 25)
                        {
                            isSuccess = false;
                            strMessage = "小于5个字符或大于25个字符！";
                        }
                        else
                        {
                            using (CBizSysUser objSysUser = new CBizSysUser())
                            {
                                DataSet ds = objSysUser.SelectByNameOrEmail(strKeyValue, "");
                                if (ds.Tables[0].Rows.Count > 0)
                                {
                                    isSuccess = false;
                                    strMessage = "该用户名已注册！";
                                }
                            }
                        }
                        break;
                    case "txtUSER_EMAIL":
                        Regex mai = new Regex(@"(?<email>[\da-zA-Z_]+@([-\dA-Za-z]+\.)+[a-zA-Z]{2,})");
                        if (!mai.IsMatch(strKeyValue))
                        {
                            isSuccess = false;
                            strMessage = "输入的邮箱帐号有误！";
                        }
                        else
                        {
                            using (CBizSysUser objSysUser = new CBizSysUser())
                            {
                                DataSet ds = objSysUser.SelectByNameOrEmail("", strKeyValue);
                                if (ds.Tables[0].Rows.Count > 0)
                                {
                                    isSuccess = false;
                                    strMessage = "该邮箱帐号已注册！";
                                }
                            }
                        }
                        break;
                    case "pwdUSER_PWD":
                        if (strKeyValue.Length < 6 || strKeyValue.Length > 16)
                        {
                            isSuccess = false;
                            strMessage = "小于6个字符或大于16个字符！";
                        }
                        else
                        {
                            Regex pwd = new Regex("^[0-9]*$");
                            if (pwd.IsMatch(strKeyValue))
                            {
                                isSuccess = false;
                                strMessage = "请使用字母加数字的组合密码！";
                            }
                            else
                            {
                                pwd = new Regex("^[a-zA-Z]*$");
                                if (pwd.IsMatch(strKeyValue))
                                {
                                    isSuccess = false;
                                    strMessage = "请使用字母加数字的组合密码！";
                                }
                                else
                                {
                                    pwd = new Regex("[0-9a-zA-Z]*");
                                    if (!pwd.IsMatch(strKeyValue))
                                    {
                                        isSuccess = false;
                                        strMessage = "请使用字母加数字的组合密码！";
                                    }
                                }
                            }
                        }
                        break;
                    case "txtYZM":
                        string strCheakCode = "";
                        if (this.Context.Request.Cookies["cheakcode"] != null)
                        {
                            strCheakCode = this.Context.Request.Cookies["cheakcode"].Value.ToString();
                        }
                        strCheakCode = msBsc.AppLogic.Cfg.DesCry.DESDecrypt(strCheakCode).ToUpper();
                        if (strKeyValue.ToUpper() != strCheakCode)
                        {
                            isSuccess = false;
                            strMessage = "验证码错误！";
                        }
                        break;
                    default:
                        this.AddCustJson("success", "\"\"");
                        break;
                }

                if (isSuccess)
                {
                    this.AddCustJson("success", "\"\"");
                }
                else
                {
                    this.AddCustJson("message", "\"" + strMessage + "\"");
                }
            }
            else if (this.strAction == "REGIST_STEP1")
            {
                using (CBizSysUser objSysUser = new CBizSysUser())
                {
                    DataSet ds = objSysUser.SelectByNameOrEmail(this.GetParamHtValue("USER_NME"), "");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        throw new Exception("用户已存在！");
                    }
                    DataRow drNew = ds.Tables[0].NewRow();
                    drNew["USER_NME"] = this.GetParamHtValue("USER_NME");
                    drNew["LOGIN_PSWD"] = this.GetParamHtValue("USER_PWD");
                    drNew["E_MAIL"] = this.GetParamHtValue("USER_EMAIL");
                    drNew["COMPANY_NME"] = "";
                    drNew["OFCE_NME"] = "";
                    drNew["USER_TYPE_CID"] = "2";//0系统用户，1商户，2客户
                    drNew["REC_ACTIVE"] = false;

                    ds.Tables[0].Rows.Add(drNew);

                    if (ds.HasChanges())
                    {
                        System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                        transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                        transactionOption.Timeout = new TimeSpan(0, 10, 0);
                        using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                        {
                            objSysUser.Update(ds);
                            ds.AcceptChanges();
                            ts.Complete();
                            ts.Dispose();
                        }
                    }
                }
                //using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
                //{

                //    string strSql = "SELECT * FROM SYS_USER WHERE USER_NME ='" + this.GetParamHtValue("USER_NME") + "'";
                //    DataSet ds = oCDbAccess.QueryBySQL(strSql);
                //    ds.Tables[0].TableName = "SYS_USER";
                //    DataRow drNew = ds.Tables[0].NewRow();
                //    drNew["USER_NME"] = oCDbAccess.NewID();
                //    drNew["USER_NME"] = this.GetParamHtValue("USER_NME");
                //    drNew["LOGIN_PSWD"] = msBsc.AppLogic.Cfg.DesCry.DESEncrypt(this.GetParamHtValue("USER_PWD"));
                //    drNew["E_MAIL"] = this.GetParamHtValue("USER_EMAIL");
                //    drNew["COMPANY_NME"] = "";
                //    drNew["OFCE_NME"] = "";
                //    drNew["USER_TYPE_CID"] = "BUYERS";
                //    drNew["REC_ACTIVE"] = false;

                //    ds.Tables[0].Rows.Add(drNew);

                //    if (ds.HasChanges())
                //    {
                //        System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                //        transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                //        transactionOption.Timeout = new TimeSpan(0, 10, 0);
                //        using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                //        {
                //            using (Epm.AppLogic.Framework.CustBizObject objtem1 = new Epm.AppLogic.Framework.CustBizObject(ds.Tables[0], "", "USER_NME"))
                //            {
                //                objtem1.Update(ds);
                //            }
                //            ds.AcceptChanges();
                //            ts.Complete();
                //            ts.Dispose();
                //        }
                //    }
                //}
                var cookie_user_nme = new HttpCookie("USER_NME", this.GetParamHtValue("USER_NME"));
                var cookie_e_mail = new HttpCookie("E_MAIL", this.GetParamHtValue("USER_EMAIL"));
                if (this.Context.Request.Url.Host.ToLower() != "localhost")
                {
                    cookie_user_nme.Domain = this.Context.Request.Url.Host;
                    cookie_e_mail.Domain = this.Context.Request.Url.Host;
                }
                cookie_user_nme.HttpOnly = false;
                cookie_e_mail.HttpOnly = false;
                this.Context.Response.Cookies.Add(cookie_user_nme);
                this.Context.Response.Cookies.Add(cookie_e_mail);
                ShowSetp("STEP2", this.GetParamHtValue("USER_NME"), this.GetParamHtValue("USER_EMAIL"));
            }
        }

        private void ShowSetp(string strStep, string strUser, string strEmail)
        {
            if (strStep == "STEP2")
            {
                string strRegist_Step2 = "<div class=\"regStepWrap\"><div class=\"regStep step2\"></div></div><div style=\"height: 50px;\"></div><div style=\"width: 600px; padding-left: 50px; float: left;\"><div style=\"height: auto;\"><div style=\"background-position: -1px -78px;\" id=\"statusIcon\"></div><div style=\"width: 500px; margin-left: 10px; float: left;\"><font style=\"color: rgb(234, 46, 73); line-height: 32px; font-size: 20px;\">" + strUser + "</font><font style=\"color: rgb(136, 136, 136); line-height: 32px; font-size: 20px;\">，恭喜注册成功！</font></div><div class=\"clear0\"></div><div class=\"clear10\"></div><div style=\"height: 30px; padding-left: 40px;\"><a id=\"regActiveBtn\" class=\"aBtn_pink aBtnBB\">马上去邮箱激活</a></div><div style=\"border-bottom-color: rgb(136, 136, 136); border-bottom-width: 1px; border-bottom-style: dashed;\" class=\"clear20\"></div></div><div class=\"regBotTip\"><p>您的注册邮箱是：<span style=\"color: rgb(234, 46, 73);\" id=\"emailWrap\">" + strEmail + "</span></p><p>请登录您的注册邮箱激活账户。我们已给您的邮箱发送了一封激活邮件，</p><p>收到后请按照提示操作。<a class=\"aLink_blue\" href=\"javascript:void(0);\">激活演示</a></p><p>没收到激活邮件？<a class=\"aLink_blue\" href=\"javascript:void(0);\">再次发送</a></p></div></div><div class=\"clear15\"></div><div style=\"height: 15px; clear: both;\"></div><form id=\"emailActiveForm\" method=\"get\" action=\"\" target=\"_blank\"></form>";
                this.ExecJScript("$(\"#mySpan\").html(''); $('.content').html('" + strRegist_Step2 + "');");
            }
        }
    }
}